<?php

namespace Frieza\Modules\Position\Controllers;

use Doraemon\model\Member;
use Doraemon\model\MemberDecoration;
use Doraemon\model\MemberExtendInfo;
use Doraemon\tools\Area;
use Encore\Admin\Admin;
use Encore\Admin\Grid\Tools\AbstractTool;
use Encore\Admin\Widgets\Form;

class UserProfileTool extends AbstractTool
{
    protected $form_id = 'grid-modal-user-profile-form';

    protected function script()
    {
        return <<<EOT
        $("#{$this->form_id}").submit(function(){

        });
EOT;
    }

    public function form()
    {
        $request = \Illuminate\Support\Facades\Request::all();
        $provinceId = \Illuminate\Support\Facades\Request::input('province', 0);
        $cityId = \Illuminate\Support\Facades\Request::input('city', 0);
        $role_type = \Illuminate\Support\Facades\Request::input('role_type', '-1');
        $house_type = \Illuminate\Support\Facades\Request::input('house_type', '-1');
        $decoration_status = \Illuminate\Support\Facades\Request::input('decoration_status', '-1');
        $day = \Illuminate\Support\Facades\Request::input('day', '');
        $version = \Illuminate\Support\Facades\Request::input('version', '');
        $info = [
            'house_type' => $house_type === '' ? '-1' : $house_type,
            'role_type' => $role_type === '' ? '-1' : $role_type,
            'decoration_status' => $decoration_status === '' ? '-1' : $decoration_status,
            'province' => $provinceId,
            'city' => $cityId,
            'day' => $day,
            'version' => $version,
        ];
        $form = new Form($info);
        $form->attribute(['id' => $this->form_id]);
        $form->select('role_type', '用户身份')->options(Member::$typeDesc);
        $form->select('decoration_status', '装修状态')->options(MemberExtendInfo::STATUS_DECORATION_MAP);
        $form->select('house_type', '房屋类型')->options(MemberDecoration::HOUSE_MAP);
        $form->number('day', '启动日');
        $form->text('version', '版本');
        $top = Area::getLocation();
        $city = $provinceId > 0 ? Area::getLocation($provinceId) : [];
        $form->select('province', '地区(一级)')->options($top)->load('city', route('position.area'));
        $form->select('city', '地区(二级)')->options($city);
        $form->method('GET');
        $form->disablePjax();
        $form->action($this->grid->resource() . '?' . http_build_query($request));
        return $form->render();
    }

    public function render()
    {
        Admin::script($this->script());
        $key = 'test';
        $html = $this->form();
        return <<<EOF
<span class="grid-expand" data-toggle="modal" data-key="{$key}" data-target="#grid-modal-user-profile">
   <a class='report-posts btn btn-sm btn-info'><i class='fa fa-search-plus'></i>用户分群筛选</a>
</span>

<div class="modal fade" id="grid-modal-user-profile" tabindex="-1" role="dialog">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">用户分群筛选</h4>
      </div>
      <div class="modal-body-{$key}">
        {$html}
      </div>
    </div>
  </div>
</div>
EOF;
    }
}
